package ru.evga.billboard.graph.preference;

import java.util.LinkedList;
import java.util.List;

public abstract class Param<T>
{
    private T defaultValue;
    private T value;
    private String key;
    
    private List<IParamChangeListener<T>> paramChangeListeners = new LinkedList<IParamChangeListener<T>>();
    
    public Param(String key, T defaultValue)
    {
        this.defaultValue = defaultValue;
        this.key = key;
    }
    
    public void addChangeListener(IParamChangeListener<T> listener)
    {
        if (!paramChangeListeners.contains(listener))
        {
            paramChangeListeners.add(listener);
        }
    }
    
    public void removeChangeListener(IParamChangeListener<T> listener)
    {
        paramChangeListeners.remove(listener);
    }
    
    private void notifyListeners()
    {
        for(IParamChangeListener<T> l : paramChangeListeners)
        {
            l.onParamChange(this);
        }
    }
    
    public T getValue()
    {
        return value == null ? defaultValue : value;
    }
    
    public T getDefaultValue()
    {
        return defaultValue;
    }
    
    public void setValue(T value)
    {
        this.value = value;
        notifyListeners();
    }
    
    public abstract void fromString(String strValue);
    
    public abstract String toString();

    public String getKey()
    {
        return key;
    }
    
}
